﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModSalesman
{
    //进场登记
    public partial class InAdd : System.Web.UI.Page
    {
        SalesmanBiz salesmanBiz = new SalesmanBiz();
        DefDicBiz defDicBiz = new DefDicBiz();

        string account = "";


        protected void Page_Load(object sender, EventArgs e)
        {
            string inID = "0";
            string saleManID = "0"; //通过人员列表界面，弹出界面来获得基本信息
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (!IsPostBack)
            {
                if (Request.Params["SaleManID"] != null)
                {
                    saleManID = Request.Params["SaleManID"].ToString();
                }
                if (Request.Params["InID"] != null)
                {
                    inID = Request.Params["InID"].ToString();
                    ViewState["VsInID"] = inID;
                    btnAdd.Enabled = false;
                }
                else
                {
                    btnUpdate.Enabled = false;
                    btnDelete.Enabled = false;
                }

                JSUtility.AddConfirm(btnDelete, "您真的要删除该入场信息吗？");
                txtBeginTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if (inID != "0") BindData(inID);
                else if (saleManID != "0")
                {
                    DataTable dt = salesmanBiz.GetOneInfo(saleManID); ;
                    FillData(dt);
                }

            }
        }
        private void BindData(string inID)
        {
            DataTable dt = salesmanBiz.GetInOneInfo(inID);
            if (dt.Rows.Count > 0)
            {
                hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtTypeName.Text = dt.Rows[0]["TypeName"].ToString();
                txtFromCompanyName.Text = dt.Rows[0]["FromCompanyName"].ToString();


                txtManageAmount.Text = dt.Rows[0]["ManageAmount"].ToString();
                txtSecurityAmount.Text = dt.Rows[0]["SecurityAmount"].ToString();

                string BeginTime = dt.Rows[0]["BeginTime"].ToString();
                if (Utils.RegexRule.IsDate(BeginTime))
                    txtBeginTime.Text = Convert.ToDateTime(BeginTime).ToString("yyyy-MM-dd");
                string EndTime = dt.Rows[0]["EndTime"].ToString();
                if (Utils.RegexRule.IsDate(EndTime))
                    txtEndTime.Text = Convert.ToDateTime(EndTime).ToString("yyyy-MM-dd");

                txtSaleTime.Text = dt.Rows[0]["SaleTime"].ToString();
                defDicBiz.InitSynDropDownList(ddlSaleArea, dt.Rows[0]["SaleArea"].ToString());
                defDicBiz.InitSynDropDownList(ddlPayWay, dt.Rows[0]["PayWay"].ToString());

                // 
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                if (dt.Rows[0]["Statu"].ToString() == "N")
                {
                    btnDelete.Enabled = false;
                    btnUpdate.Enabled = false;
                }
                dt.Dispose();
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string hidID = hidSaleManID.Value;
            if (string.IsNullOrEmpty(hidID))
            {

                JSUtility.Alert(this, "操作错误：请从列表界面进入、或点后面的查询获取人员信息！");
                return;

            }
            string empCode = txtEmpCode.Text.Trim();
            //根据工号获取在职状态 如果是离职变为在职 就要检测重新输入的工号是否多人使用
            DataTable dthid = salesmanBiz.GetOneInfo(hidID);
            if (dthid.Rows[0]["StateName"].ToString() == "离职" && empCode != dthid.Rows[0]["EmpCode"].ToString())
            {
                if (salesmanBiz.IsExistEmpCode(empCode, "Y"))
                {
                    JSUtility.Alert(this, "已经有在职售货员使用此工号，请先让其离职");
                    return;
                }
            }
            if (salesmanBiz.IsExistInInfo(empCode))
            {
                JSUtility.Alert(this, "已经存在该售货员有效入场信息");
                return;
            }
            string BeginTime = txtBeginTime.Text.Trim();
            if (!RegexRule.IsDate(BeginTime))
            {
                JSUtility.Alert(this, "入场时间请输入正确的日期格式！");
                return;
            }
            string EndTime = txtEndTime.Text.Trim();
            if (!string.IsNullOrEmpty(EndTime))
            {
                if (!RegexRule.IsDate(EndTime))
                {
                    JSUtility.Alert(this, "截止时间请输入正确的日期格式！");
                    return;
                }
            }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("SaleManID", hidID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("TypeName", txtTypeName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("FromCompanyName", txtFromCompanyName.Text.Trim());

            sdList.Add("SaleTime", txtSaleTime.Text.Trim());
            sdList.Add("ManageAmount", txtManageAmount.Text.Trim());
            sdList.Add("SecurityAmount", txtSecurityAmount.Text.Trim());
            sdList.Add("BeginTime", BeginTime);
            sdList.Add("EndTime", EndTime);
            sdList.Add("PayWay", ddlPayWay.SelectedValue);
            sdList.Add("SaleArea", ddlSaleArea.SelectedValue);

            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);

            SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
            hList.Add("SaleManID", hidID);
            hList.Add("EmpCode", empCode);
            hList.Add("DeptName", txtDeptName.Text.Trim());
            hList.Add("TypeName", txtTypeName.Text.Trim());
            hList.Add("DutyName", txtDutyName.Text.Trim());
            hList.Add("FromCompanyName", txtFromCompanyName.Text.Trim());
            hList.Add("StateName", "在职");//字典中的ID 
            hList.Add("ManageAmount", txtManageAmount.Text.Trim());
            hList.Add("SecurityAmount", txtSecurityAmount.Text.Trim());
            hList.Add("InTime", BeginTime);//进场时间
            hList.Add("OutTime", EndTime);//退场时间
            hList.Add("DayNum", "null");//移交时间
            hList.Add("LeaveTime", "null");//未使用
            if (salesmanBiz.Update("Admin_Salesman", hList))
            {
                if (salesmanBiz.Insert("Admin_Salesman_In", sdList))
                    JSUtility.AlertAndRedirect("增加促销员入场信息成功，更新基本信息成功!", "InAdd.aspx");
                else
                    JSUtility.Alert(this, "增加促销员入场信息失败[成功更新了促销员基本资料]");
            }
            else
                JSUtility.Alert(this, "更新基本资料失败[未增加入场职信息]");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string inID = ViewState["VsInID"].ToString();
            if (inID == "0")
            {
                JSUtility.Alert(this, "操作错误：没有正确加载要更新的信息！");
                return;

            }
            string empCode = txtEmpCode.Text.Trim();
            string BeginTime = txtBeginTime.Text.Trim();
            if (!RegexRule.IsDate(BeginTime))
            {
                JSUtility.Alert(this, "离职时间请输入正确的日期格式！");
                return;
            }
            string EndTime = txtEndTime.Text.Trim();
            if (!string.IsNullOrEmpty(EndTime))
            {
                if (!RegexRule.IsDate(EndTime))
                {
                    JSUtility.Alert(this, "截止时间请输入正确的日期格式！");
                    return;
                }
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("InID", inID);
            sdList.Add("SaleManID", hidSaleManID.Value);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("TypeName", txtTypeName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("FromCompanyName", txtFromCompanyName.Text.Trim());

            sdList.Add("SaleTime", txtSaleTime.Text.Trim());
            sdList.Add("ManageAmount", txtManageAmount.Text.Trim());
            sdList.Add("SecurityAmount", txtSecurityAmount.Text.Trim());
            sdList.Add("BeginTime", BeginTime);
            sdList.Add("EndTime", EndTime);
            sdList.Add("PayWay", ddlPayWay.SelectedValue);
            sdList.Add("SaleArea", ddlSaleArea.SelectedValue);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (inID != "0")
            {
                if (salesmanBiz.Update("Admin_Salesman_In", sdList))
                {
                    SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
                    hList.Add("SaleManID", hidSaleManID.Value);
                    hList.Add("EmpCode", empCode);
                    hList.Add("DeptName", txtDeptName.Text.Trim());
                    hList.Add("TypeName", txtTypeName.Text.Trim());
                    hList.Add("DutyName", txtDutyName.Text.Trim());
                    hList.Add("FromCompanyName", txtFromCompanyName.Text.Trim());
                    hList.Add("StateName", "在职");//字典中的ID 
                    hList.Add("ManageAmount", txtManageAmount.Text.Trim());
                    hList.Add("SecurityAmount", txtSecurityAmount.Text.Trim());
                    hList.Add("InTime", BeginTime);//进场时间
                    hList.Add("OutTime", EndTime);//退场时间
                    //hList.Add("DayNum", "null");//移交时间
                    //hList.Add("LeaveTime", "null");//未使用
                    salesmanBiz.Update("Admin_Salesman", hList);
                    BindData(inID);
                    JSUtility.Alert(this, "成功更新入场信息");
                }
                else
                    JSUtility.Alert(this, "更新入场信息失败");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string inID = ViewState["VsInID"].ToString();
            string info = "";
            if (inID != "0")
            {
                if (salesmanBiz.DeleteInOneInfo(inID))
                    info = "删除成功";
                else
                    info = "删除失败";
            }
            else
            { info = "请先选择记录"; }

            JSUtility.Alert(this, info);
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

        /*
        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = salesmanBiz.GetOneInfoByName(empCode,"离职");
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        FillData(dt);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = salesmanBiz.GetOneInfo(empCode, "离职");
                    if (dt.Rows.Count > 0)
                    {
                        FillData(dt);
                    }
                }
            }



        }
       
        */
        private void FillData(DataTable dt)
        {
            hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
            txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
            txtTypeName.Text = dt.Rows[0]["TypeName"].ToString();
            txtFromCompanyName.Text = dt.Rows[0]["FromCompanyName"].ToString();

            txtManageAmount.Text = dt.Rows[0]["ManageAmount"].ToString();
            txtSecurityAmount.Text = dt.Rows[0]["SecurityAmount"].ToString();

        }
    }
}